package com.plugins.http;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpHost;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iclient.dao.DaoUtils;
import com.iclient.dao.model.MCookie;
import com.iclient.dao.service.ImgService;

public class LocalCookie {

	private static List<String> keys = new ArrayList<>();
	static{
		keys.add("SUB");
		keys.add("_T_WM");
		keys.add("gsid_CTandWM");
	}
	
	public static CookieStore getCookie(String cookieText){
		JSONArray jsonArray = JSONArray.parseArray(cookieText);
		final CookieStore cookieStore = new BasicCookieStore();
		for(int i=0; i<jsonArray.size(); i++){
			JSONObject obj = jsonArray.getJSONObject(i);
			String name = obj.getString("name");
			if(obj.getBoolean("persistent") && keys.contains(name)){
				BasicClientCookie basicClientCookie = new BasicClientCookie(name, obj.getString("value"));
				basicClientCookie.setExpiryDate(new Date(obj.getLongValue("expiryDate")));
				basicClientCookie.setVersion(obj.getIntValue("version"));
				basicClientCookie.setPath(obj.getString("path"));
	            basicClientCookie.setDomain(obj.getString("domain"));
	            basicClientCookie.setSecure(obj.getBooleanValue("secure"));
	            basicClientCookie.setAttribute("persistent", "true");
	            cookieStore.addCookie(basicClientCookie);
			}
		}
		return cookieStore;
	}
	
	public static Boolean isValid(String cookieText){
		try{
			CookieStore cookieStore = LocalCookie.getCookie(cookieText);
			HttpClientContext httpContext = HttpClientContext.create();
			httpContext.setCookieStore(cookieStore);
			HttpGet get = new HttpGet("http://weibo.cn/");
			CloseableHttpClient httpClient = HttpClients.createDefault();
			RequestConfig config = RequestConfig.custom()
					.setProxy(new HttpHost("127.0.0.1", 8888))
					.setCookieSpec(CookieSpecs.STANDARD)
					.setRedirectsEnabled(false)
					.build();
			get.setConfig(config);
			CloseableHttpResponse httpResponse = httpClient.execute(get, httpContext);
			Integer statuscode = httpResponse.getStatusLine().getStatusCode();
			httpClient.close();
			System.out.println(statuscode);
			if(statuscode == 200){
				return true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
	
	public static void main(String[] args) {
		ImgService imgService = (ImgService) DaoUtils.getBean("imgService");
		MCookie mcookie = imgService.findCookie("msinalogin");
		System.out.println(isValid(mcookie.getContent()));
	}
}
